import time

from io import  BytesIO

import requests
from PIL import Image
from selenium import webdriver
from browsermobproxy import Server

from selenium.webdriver.chrome.options import Options

ops = {}
ops.setdefault("port",2333)
server = Server(r'E:\Python\Tools\browsermob-proxy-2.1.4\bin\browsermob-proxy.bat',options=ops)
server.start()
proxy = server.create_proxy()

chrome_options = Options()
chrome_options.add_argument('--proxy-server={0}'.format(proxy.proxy))

driver = webdriver.Chrome(chrome_options=chrome_options)
#PPT
base_url = "https://max.book118.com/html/2019/0214/6055044130002010.shtm"
proxy.new_har("douyin", options={'captureHeaders': True, 'captureContent': True})
driver.get(base_url)

look =  driver.find_element_by_id('agree_full')
look.click()
driver.switch_to.frame('layer_view_iframe')
time.sleep(3)

index = 1
listlen = len([element for element in proxy.har['log']['entries'] if "wKh2B" in element['request']['url']
               or "wKh2C" in element['request']['url'] or '/img/image' in element['request']['url']])
while(True):
    pageIndex = driver.find_element_by_id('PageIndex').text
    pageCount = driver.find_element_by_id('PageCount').text
    if(pageIndex!=pageCount):
        temp = len([element for element in proxy.har['log']['entries'] if "wKh2B" in element['request']['url']
                    or "wKh2C" in element['request']['url'] or '/img/image' in element['request']['url']])
        print(listlen,"===============",temp)
        if(listlen==2 or listlen < temp):
            _next = driver.find_element_by_id('btmRight')
            _next.click()
            listlen = temp
    time.sleep(3)
print("++++++++++start++++++")
result = proxy.har['log']['entries']
for entry in result:
    _url = entry['request']['url']
    # 根据URL找到数据接口
    if "wKh2B" in _url or "wKh2C" in _url or '/img/image' in _url:
        _url = entry['request']['url']
        response = requests.get(_url)
        image = Image.open(BytesIO(response.content))
        # image.show()
        if image.mode == "P":
            image = image.convert('RGB')
        # filepath = 'E:/other/自考专升本/03708中国近现代史纲要/%s.jpg' % str(index)
        filepath = 'C:/Users/clq/Desktop/Word/%s.jpg' % str(index)
        image.save(filepath)
        index = index+1
server.stop()
driver.quit()